<template>
  <div class="pagination">
    <button :disabled="currentPageNo===1" @click="$emit('changePageNo',currentPageNo-1)">上一页</button>
    <button v-if="startEnd.start>1" @click="$emit('changePageNo',1)">1</button>
    <button v-if="startEnd.start>2">···</button>

    <button
        :class="{active:currentPageNo===page}"
        @click="$emit('changePageNo',page)"
        v-for="page in startEnd.end" :key="page"
        v-if="page>=startEnd.start">
      {{ page }}
    </button>
    <!--    <button>4</button>-->
    <!--    <button>5</button>-->
    <!--    <button>6</button>-->
    <!--    <button>7</button>-->

    <button v-if="startEnd.end<totalPageNo-1">···</button>
    <button v-if="startEnd.end<totalPageNo" @click="$emit('changePageNo',totalPageNo)">{{ totalPageNo }}</button>
    <button :disabled="currentPageNo===totalPageNo" @click="$emit('changePageNo',currentPageNo+1)">下一页</button>

    <button style="margin-left: 30px">共{{ total }}条</button>
  </div>
</template>

<script>

export default {
  name: "Pagination",
  props: {
    currentPageNo: {//当前页数
      type: Number,
      default: 1,
    },
    pageSize: {//每一页显示几个
      type: Number,
      default: 0
    },
    total: {//一共多少个商品
      type: Number,
      default: 0
    },
    contineNo: Number,//分页器中间显示几个

  },
  computed: {
    totalPageNo() {//一共多少页
      return Math.ceil(this.total / this.pageSize)
    },
    startEnd() {//如何显示页数，中间的页数 开始又是多少，结束又是多少
      const {totalPageNo, contineNo, currentPageNo} = this;

      let start, end;
      if (totalPageNo <= contineNo) {
        //如果总页数小于中间需要显示分页数,那么一共就只需要这几页就行了
        start = 1;
        end = totalPageNo
      } else {
        start = currentPageNo - Math.floor(contineNo / 2);
        end = currentPageNo + Math.floor(contineNo / 2);
        //如果总的页数大于中间需要的中继页数，那么需要继续判断
        // 1 2   3 4 5 6 7   8 9 10 11
        //假设中继页数为5个 如果当前页数小于3，也就是最前面是1 2 3 4 5 ....11
        if (currentPageNo < Math.ceil(contineNo / 2)) {
          start = 1;
          end = contineNo;
        }
        //假设中继页数为5个 如果当前页数大于9，也就是最前面是1....7 8 9 10 11
        if (currentPageNo > totalPageNo - Math.ceil(contineNo / 2)) {
          start = totalPageNo - contineNo + 1;
          end = totalPageNo;
        }
      }

      return {start, end};

    }
  },

}
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>



